package com.thread;

/**
 * Created by john on 2016/4/5.
 */
public class SynchronizedThreadTest2 extends  Thread{

       private  int count = 30000;

    @Override
     synchronized  public void run() {
        System.out.println(Thread.currentThread().getName()+"-isInterrupted:"+this.isInterrupted());
          while(count > 0 && !Thread.currentThread().isInterrupted()){
              count --;
              System.out.println("由线程"+Thread.currentThread().getName()+"计算，count="+count);
          }
    }

    public static void main(String[] args) throws InterruptedException {
        SynchronizedThreadTest2 t1 = new SynchronizedThreadTest2();
     /*   t1.start();
        Thread.sleep(1000);
        t1.interrupt();
        System.out.println(t1.isInterrupted());
        System.out.println(t1.isInterrupted());
*/

        // 除了A线程能获得资源进行执行，B在A被打断后也能获取到资源执行--执行数据量比较大才能得到理想测试结果
        Thread a = new Thread(t1,"A");
        a.start();
        Thread.sleep(100);
        a.interrupt();
        new Thread(t1,"B").start();
        new Thread(t1,"C").start();

        /*Thread a = new Thread(t1,"A");
        a.start();
        Thread.sleep(1);
        a.interrupt();
        new Thread(t1,"B").start();
        new Thread(t1,"C").start();
        System.out.println("Thread.interrupted:"+Thread.interrupted());//判断main线程是否中断
        System.out.println("a.isInterrupted:"+a.isInterrupted());//判断a线程是否中断
        System.out.println("a.isInterrupted:"+a.isInterrupted());//判断a线程是否中断*/
    }

}
